package com.hfh.net;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author hfh
 * @Date 2022 09 13 17 16
 **/
public class Test5 {
    @Test
    public void server() throws IOException {
        ServerSocket ss = new ServerSocket(9999);

        Socket s =ss.accept();
        InputStream is = s.getInputStream();
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("b.txt"));

        byte[] arr = new byte[1024];
        int len;
        while ((len= is.read(arr))!=-1){
            String str = new String(arr,0,len);
            str = str.toUpperCase();
            bos.write(str.getBytes());
        }

        bos.close();
        s.close();
        ss.close();
    }

    @Test
    public void client() throws IOException {
        Socket s = new Socket(InetAddress.getLocalHost(),9999);

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
        OutputStream os = s.getOutputStream();

        byte[] arr = new byte[1024];
        int len;
        while ((len= bis.read(arr))!=-1){
            os.write(arr,0,len);
        }

        os.close();
        bis.close();
        s.close();
    }
}
